﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace SwormTests.IUD
{
    public class by_attributed_pk : TestBase
    {
        public by_attributed_pk(DbToTest db) : base(db) { }

        class custom_pk
        {
            [Sworm.Column(IsPrimaryKey = true)]
            public string the_pk { get; set; }

            public string another_field { get; set; }
        }

        [Test]
        public void asd902jc()
        {
            Assert.Inconclusive("TODO create tests for ColumnAttribute - Name and DbType");
        }

        [Test]
        public void insert_and_update()
        {
            var record = new custom_pk() { the_pk = "pk", another_field = "hello" };
            DB.Insert(record);
            record = DB.Query.Simple<custom_pk>().Single("pk");
            Assert.AreEqual("hello", record.another_field);

            int rowsAffected = DB.Update(new custom_pk() { the_pk = "pk", another_field = "version 2" });
            Assert.AreEqual(1, rowsAffected);
            record = DB.Query.Simple<custom_pk>().Single("pk");
            Assert.AreEqual("version 2", record.another_field);
        }

        [Test]
        public void insert_and_delete()
        {
            DB.Insert(new custom_pk() { the_pk = "one", another_field = "1" });
            DB.Insert(new custom_pk() { the_pk = "two", another_field = "2" });
            Assert.AreEqual(2, DB.Query.Simple<custom_pk>().List().Count);

            int rowsAffected = DB.Delete(new custom_pk() { the_pk = "one" });
            Assert.AreEqual(1, rowsAffected);
            var records = DB.Query.Simple<custom_pk>().List();
            Assert.AreEqual(1, records.Count);
            Assert.AreEqual("two", records[0].the_pk);
        }
    }
}
